package net.csdn.business.discuss.api.facade;

import lombok.extern.slf4j.Slf4j;
import net.csdn.business.discuss.api.enums.AutditActionEventEnum;
import net.csdn.business.discuss.api.enums.DiscussActionEventEnum;
import net.csdn.business.discuss.api.model.dto.*;
import net.csdn.business.discuss.api.model.vo.DiscussCommentVO;
import net.csdn.business.discuss.api.model.vo.DiscussVO;
import net.csdn.business.discuss.api.service.IDiscussActivityService;
import net.csdn.business.discuss.api.service.IDiscussAuditService;
import net.csdn.business.discuss.api.service.IDiscussCommentService;
import net.csdn.business.discuss.api.service.IDiscussService;
import net.csdn.business.discuss.common.enums.ResultCodeEnum;
import net.csdn.business.discuss.common.exception.BusinessException;
import net.csdn.business.discuss.common.model.dto.SendDiscussAuditDTO;
import net.csdn.business.discuss.common.model.dto.SendDiscussCommentAuditDTO;
import net.csdn.business.discuss.common.model.dto.UserRoleDto;
import net.csdn.business.discuss.feign.api.OrgFeignClient;
import net.csdn.business.discuss.feign.api.ProjectFeignClient;
import net.csdn.business.discuss.feign.api.UserFeignClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @ClassName DiscussWrapperFacade
 * @Description TODO
 * @Author zhangyl
 * @Date 2023/7/29 17:02
 */
@Slf4j
@Component
public class DiscussCommentWrapperFacade {

    @Autowired
    private IDiscussService discussService;

    @Autowired
    private IDiscussCommentService discussCommentService;

    @Autowired
    private IDiscussActivityService discussActivityService;

    @Autowired
    private IDiscussAuditService discussAuditService;

    @Autowired
    private OrgFeignClient orgFeignClient;

    @Autowired
    private ProjectFeignClient projectFeignClient;


    public String add(NewDiscussCommentDTO params){
        String discussId=params.getDiscussId();
        String currUserId=params.getCurrUserId();
        DiscussVO discussVO=discussService.selectDiscussById(discussId);
        if(discussVO==null){
            throw new BusinessException(ResultCodeEnum.DISCUSS_NO_EXIST_ERROR);
        }
        Integer isClosed=discussVO.getIsClosed();
        /*if(isClosed==1){
            throw new BusinessException(ResultCodeEnum.DISCUSS_CLOSE_ERROR);
        }*/
        //如果讨论被锁定，则判断当前用户是否是组织或者项目成员，如果是组织成员可以发表评论
        Integer isLocked=discussVO.getIsLock();
        Integer sourceType=discussVO.getSourceType();
        String sourceId=discussVO.getSourceId();
        if(isLocked==1){
            Integer acessLevel=0;
            UserRoleDto acessParams=new UserRoleDto();
            acessParams.setCurrUserId(currUserId);
            //判断组织或项目的访问权限
            try{
                switch (sourceType){
                    case 1:
                        acessParams.setOrgId(sourceId);
                        acessLevel=orgFeignClient.getOrgAccessLevel(acessParams);
                        break;
                    case 2:
                        acessParams.setProjectId(sourceId);
                        acessLevel=projectFeignClient.getProjectAccessLevel(acessParams);
                        break;
                }
            }catch (Exception e){
                log.error("-----查询项目或组织成员的访问权限出错");
            }
            if(acessLevel<10){
                throw new BusinessException(ResultCodeEnum.DISCUSS_LOCK_ERROR);
            }
        }
        params.setSourceId(sourceId);
        String result=discussCommentService.insertDiscussComment(params);
        //发送评论事件
        CommentCreateActivityDTO createActivity=new CommentCreateActivityDTO();
        createActivity.setDiscussId(discussId);
        createActivity.setSourceId(discussVO.getSourceId());
        createActivity.setActionUid(currUserId);
        createActivity.setSourceType(discussVO.getSourceType());
        createActivity.setCommentAts(params.getCommentAts());
        createActivity.setCommentId(result);
        DiscussActivityEventDTO eventDTO=DiscussActivityEventDTO.build(DiscussActionEventEnum.CREATE_COMMENT,createActivity);
        discussActivityService.sendDiscussActivity(eventDTO);
        /*//发送discuss艾特用户事件
        List<String> commentAts=;
        if(commentAts!=null&&commentAts.size()>0){
            DiscussAtActivityDTO atActivityDTO=new DiscussAtActivityDTO();
            atActivityDTO.setDiscussId(discussId);
            atActivityDTO.setSourceId(sourceId);
            atActivityDTO.setActionUid(currUserId);
            atActivityDTO.setSourceType(sourceType);
            atActivityDTO.setSerialNumber(discussVO.getSerialNumber().toString());
            atActivityDTO.setContent(params.getMdContent());
            atActivityDTO.setTitle(discussVO.getTitle());
            atActivityDTO.setCommentAts(params.getCommentAts());
            DiscussActivityEventDTO atEventDTO=DiscussActivityEventDTO.build(DiscussActionEventEnum.COMMENT_AT,atActivityDTO);
            discussActivityService.sendDiscussActivity(atEventDTO);
        }*/
        //发送discuss审核
        SendDiscussCommentAuditDTO dto=new SendDiscussCommentAuditDTO();
        BeanUtils.copyProperties(params,dto);
        dto.setId(result);
        dto.setDiscussId(discussId);
        dto.setSourceId(sourceId);
        dto.setSourceType(sourceType);
        dto.setSerialNumber(discussVO.getSerialNumber().toString());
        dto.setAction(AutditActionEventEnum.DiSCUSS_COMMENT_CREATE.getCode());
        dto.setCreatedBy(currUserId);
        dto.setCreatedDate(new Date());
        AuditEventDTO event=AuditEventDTO.build(AutditActionEventEnum.DiSCUSS_COMMENT_CREATE,dto);
        discussAuditService.sendDiscussAudit(event);
        return result;
    }

    public Integer edit(UpdateDiscussCommentDTO params){
        String id=params.getId();
        String currUserId=params.getCurrUserId();
        DiscussCommentVO discussCommentVO=discussCommentService.selectDiscussCommentById(id);
        if(discussCommentVO==null){
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM,"id");
        }
        String createBy=discussCommentVO.getCreatedBy();
        if(!createBy.equals(currUserId)){
            throw new BusinessException(ResultCodeEnum.UNAUTHORIZED);
        }
        String discussId=discussCommentVO.getDiscussId();
        DiscussVO discussVO=discussService.selectDiscussById(discussId);
        if(discussVO==null){
            throw new BusinessException(ResultCodeEnum.DISCUSS_NO_EXIST_ERROR);
        }
        Integer isClosed=discussVO.getIsClosed();
        if(isClosed==1){
            throw new BusinessException(ResultCodeEnum.DISCUSS_CLOSE_ERROR);
        }
        //如果讨论被锁定，则判断当前用户是否是组织或者项目成员，如果是组织成员可以发表评论
        Integer isLocked=discussVO.getIsLock();
        Integer sourceType=discussVO.getSourceType();
        String sourceId=discussVO.getSourceId();
        if(isLocked==1){
            Integer acessLevel=0;
            UserRoleDto acessParams=new UserRoleDto();
            acessParams.setCurrUserId(currUserId);
            //判断组织或项目的访问权限
            try{
                switch (sourceType){
                    case 1:
                        acessParams.setOrgId(sourceId);
                        acessLevel=orgFeignClient.getOrgAccessLevel(acessParams);
                        break;
                    case 2:
                        acessParams.setProjectId(sourceId);
                        acessLevel=projectFeignClient.getProjectAccessLevel(acessParams);
                        break;
                }
            }catch (Exception e){
                log.error("-----查询项目或组织成员的访问权限出错");
            }
            if(acessLevel<10){
                throw new BusinessException(ResultCodeEnum.DISCUSS_LOCK_ERROR);
            }
        }
        params.setSourceId(sourceId);
        Integer result=discussCommentService.updateDiscussComment(params);
        if(result>0){
            //发送活动事件
            CommentUpdateActivityDTO udapateActivity=new CommentUpdateActivityDTO();
            udapateActivity.setDiscussId(discussId);
            udapateActivity.setSourceId(discussCommentVO.getSourceId());
            udapateActivity.setActionUid(params.getCurrUserId());
            udapateActivity.setSourceType(discussCommentVO.getSourceType());
            udapateActivity.setCommentAts(params.getCommentAts());
            udapateActivity.setCommentId(id);
            DiscussActivityEventDTO eventDTO=DiscussActivityEventDTO.build(DiscussActionEventEnum.UPDATE_COMMENT,udapateActivity);
            discussActivityService.sendDiscussActivity(eventDTO);
           /* //发送discuss艾特用户事件
            List<String> commentAts=params.getCommentAts();
            if(commentAts!=null&&commentAts.size()>0){
                DiscussAtActivityDTO atActivityDTO=new DiscussAtActivityDTO();
                atActivityDTO.setDiscussId(discussId);
                atActivityDTO.setSourceId(sourceId);
                atActivityDTO.setActionUid(currUserId);
                atActivityDTO.setSourceType(sourceType);
                atActivityDTO.setSerialNumber(discussVO.getSerialNumber().toString());
                atActivityDTO.setContent(params.getMdContent());
                atActivityDTO.setTitle(discussVO.getTitle());
                atActivityDTO.setCommentAts(params.getCommentAts());
                DiscussActivityEventDTO atEventDTO=DiscussActivityEventDTO.build(DiscussActionEventEnum.COMMENT_AT,atActivityDTO);
                discussActivityService.sendDiscussActivity(atEventDTO);
            }*/
            //发送discuss审核
            SendDiscussCommentAuditDTO dto=new SendDiscussCommentAuditDTO();
            BeanUtils.copyProperties(params,dto);
            dto.setId(id);
            dto.setSourceId(sourceId);
            dto.setDiscussId(discussId);
            dto.setSourceType(sourceType);
            dto.setSerialNumber(discussVO.getSerialNumber().toString());
            dto.setAction(AutditActionEventEnum.DiSCUSS_COMMENT_UPDATE.getCode());
            dto.setCreatedBy(params.getCurrUserId());
            dto.setCreatedDate(new Date());
            AuditEventDTO event=AuditEventDTO.build(AutditActionEventEnum.DiSCUSS_COMMENT_UPDATE,dto);
            discussAuditService.sendDiscussAudit(event);
        }
        return result;
    }


    public Integer remove(DeleteResourceDTO params){
        String id=params.getId();
        String currUserId=params.getCurrUserId();
        DiscussCommentVO discussCommentVO=discussCommentService.selectDiscussCommentById(id);
        if(discussCommentVO==null){
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM,"id");
        }
        String createBy=discussCommentVO.getCreatedBy();
        String discussId=discussCommentVO.getDiscussId();
        DiscussVO discussVO=discussService.selectDiscussById(discussId);
        if(discussVO==null){
            throw new BusinessException(ResultCodeEnum.DISCUSS_NO_EXIST_ERROR);
        }
        Integer sourceType=discussVO.getSourceType();
        String sourceId=discussVO.getSourceId();
        //非本人管理员才能删除评论
        if(!createBy.equals(currUserId)){
            Integer acessLevel=0;
            UserRoleDto acessParams=new UserRoleDto();
            acessParams.setCurrUserId(currUserId);
            //判断组织或项目的访问权限
            try{
                switch (sourceType){
                    case 1:
                        acessParams.setOrgId(sourceId);
                        acessLevel=orgFeignClient.getOrgAccessLevel(acessParams);
                        break;
                    case 2:
                        acessParams.setProjectId(sourceId);
                        acessLevel=projectFeignClient.getProjectAccessLevel(acessParams);
                        break;
                }
            }catch (Exception e){
                log.error("-----查询项目或组织成员的访问权限出错");
            }
            if(acessLevel<40){
                throw new BusinessException(ResultCodeEnum.UNAUTHORIZED);
            }
        }
        params.setSourceId(sourceId);
        Integer result=discussCommentService.deleteDiscussCommentById(params);
        if(result>0&&discussCommentVO!=null){
            //发送discuss审核
            SendDiscussCommentAuditDTO dto=new SendDiscussCommentAuditDTO();
            BeanUtils.copyProperties(discussCommentVO,dto);
            dto.setId(id);
            dto.setSourceId(sourceId);
            dto.setSourceType(sourceType);
            dto.setSerialNumber(discussVO.getSerialNumber().toString());
            dto.setAction(AutditActionEventEnum.DiSCUSS_COMMENT_DELETE.getCode());
            dto.setCreatedBy(currUserId);
            dto.setCreatedDate(new Date());
            AuditEventDTO event=AuditEventDTO.build(AutditActionEventEnum.DiSCUSS_COMMENT_DELETE,dto);
            discussAuditService.sendDiscussAudit(event);
        }
        return result;
    }
}
